package com.hinnotek.warehouse.ui

import android.os.Bundle
import androidx.activity.viewModels
import androidx.core.widget.addTextChangedListener
import androidx.lifecycle.Observer
import com.alibaba.android.arouter.facade.annotation.Route
import com.hinnotek.warehouse.BR
import com.hinnotek.warehouse.R
import com.hinnotek.warehouse.bean.WareHouseDetailBean
import com.hinnotek.warehouse.databinding.ActivitySupplierInfoBinding
import com.hinnotek.warehouse.databinding.ItemSupplierInfoLayoutBinding
import com.hinnotek.warehouse.viewmodel.MachineAccountViewModel
import com.lee.common.adapter.binding.SimpleBindingAdapter
import com.lee.common.base.BaseActivity
import com.lee.common.ext.init
import com.lee.common.observer.DefaultObserver
import com.lee.common.utils.ACTIVITY_WAREHOUSE_SUPPLIER_INFO
import com.lee.common.utils.Skipping
import dagger.hilt.android.AndroidEntryPoint

/**
 * 供应商信息查询
 */
@Route(path = ACTIVITY_WAREHOUSE_SUPPLIER_INFO)
@AndroidEntryPoint
class SupplierInfoActivity : BaseActivity<ActivitySupplierInfoBinding>() {

    private lateinit var adapter: SimpleBindingAdapter<WareHouseDetailBean, ItemSupplierInfoLayoutBinding>
    private val viewModel by viewModels<MachineAccountViewModel>()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_supplier_info)
        setTitle("供应商")
        bindingView.vm = viewModel
        bindingView.executePendingBindings()
        viewModel.attachLoading(loadingState)
        viewModel.dialogMsg.observe(this, DefaultObserver(this))

        viewModel.autoRefresh()
    }

    override fun initVew() {

        adapter = SimpleBindingAdapter(
            R.layout.item_supplier_info_layout,
            ArrayList(),
            BR.supplierInfo
        )
        bindingView.recycler.init()
        bindingView.recycler.adapter = adapter
        emptyView.adapter = adapter
        viewModel.isEmpty.observe(this, Observer {
            emptyView.values(it)
        })
        emptyView.getErrView().setOnClickListener {
            viewModel.autoRefresh()
        }

        adapter.addListener { binding, item ->
            binding.root.setOnClickListener {
                //启停井模块 type=设备类型  num=产品序列号
                //val url = "https://amoeba.hinnotek.com/amoeba/wx/getProduct?type=02&num=1234567"
                val url = "https://amoeba.hinnotek.com/amoeba/wx/getProduct?" + "type=" + item.deviceType + "&num=" + item.productNum
                Skipping.navWeb(url, this)
            }
        }
        bindingView.name.addTextChangedListener {
            viewModel.deviceName = it.toString()
        }

        bindingView.model.addTextChangedListener {
            viewModel.deviceModel = it.toString()
        }

        bindingView.search.setOnClickListener {
            if (viewModel.deviceName != "" || viewModel.deviceModel != "") {
                viewModel.autoRefresh()
            }
        }

    }

    override fun setData() {
        viewModel.deviceInfoPage.observe(this, Observer {
            adapter.addAll(it, viewModel.isFirst)
        })
    }
}